﻿using System;
using System.Xml.Xsl;
using Demo.Services.Contracts.DTO.Filters;
using Demo.CommonBase.View.Forms;
using Ext.Net;
using Demo.Services.Services.Impl.Managers;
using Demo.CommonBase.DTO.Infos;
using Demo.Services.Contracts.DTO.Infos;
using Demo.CommonBase.Logging;
using Demo.CommonBase.Exceptions;
using System.Reflection;

namespace Demo.Web.Catalogs
{
    public partial class Project : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
            }
        }
        private ProyectFilter GetFilter()
        {
            ProyectFilter filter = new ProyectFilter();
            filter.Name = txtName.Text;
            filter.DateStart = txtDateStart.SelectedValue == null ? string.Empty : GetFormatDate(txtDateStart.SelectedValue.ToString());
            filter.DateEnd = txtDateEnd.SelectedValue == null ? string.Empty : GetFormatDate(txtDateEnd.SelectedValue.ToString());
            filter.Hours = txtHours.Text == string.Empty ? 0 : Convert.ToInt32(txtHours.Text);
            filter.StatusId = cboStatus.SelectedItem == null ? 0 : Convert.ToInt32(cboStatus.SelectedItem.Value);
            return filter;
        }
        protected void DsProjectReadData(object sender, StoreReadDataEventArgs e)
        {
            ProyectFilter filter;
            ProyectManager proyectManager;
            PaginationInfo proyectPaging;
            ProjectResultInfo result;
            try
            {
                Logger.Info();
                filter = GetFilter();
                proyectPaging = new PaginationInfo();

                proyectPaging.PageNum = e.Start;
                proyectPaging.PageSize = e.Limit;
                DataSorter sort = e.Sort[0];
                proyectPaging.Sort = sort.Property;
                proyectPaging.Direction = sort.Direction.ToString();

                proyectManager = new ProyectManager();

                result = proyectManager.GetProyectList(proyectPaging, filter);
                e.Total = result.TotalReg;
                gpProject.GetStore().DataSource = result.ProjectList;
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodBase.GetCurrentMethod(), ex);
            }
        }
        protected void BtnSearchClick(object sender, DirectEventArgs e)
        {
            dsProject.RemoveAll();
            dsProject.Reload();
        }
        protected void BtnDeleteConfirmClick(object sender, DirectEventArgs e)
        {
            int id = 0;
            if (e.ExtraParams["id"] != null)
            {
                id = Convert.ToInt32(e.ExtraParams["id"]);
            }

            ExtNet.Msg.Confirm("Confirmacion", "Desea Borrar Registro?", new MessageBoxButtonsConfig
            {
                Yes = new MessageBoxButtonConfig
                {
                    Handler = string.Format("Demo.DoYes({0})", id),
                    Text = "Si"
                },
                No = new MessageBoxButtonConfig
                {
                    Handler = "Demo.DoNothing()",
                    Text = "No",
                }
            }).Show();
        }
        [DirectMethod]
        public void DoYes(int id)
        {
            ProyectManager proyectManager;

            proyectManager = new ProyectManager();
            proyectManager.DeleteProyect(id);
            // StatusBarStatusConfig statusConfig = new StatusBarStatusConfig();
            // statusConfig.Icon = Icon.Accept;
            // statusConfig.Text = "El registro fué borrado";
            // statusConfig.Clear2 = true;
            //  FormStatusBar.SetStatus(statusConfig);
            Notification.Show(new NotificationConfig
            {
                Title = "Información",
                Icon = Icon.Information,
                Html = "El registro fué borrado",
                HideDelay = 3000
            });
            dsProject.RemoveAll();
            dsProject.Reload();
        }

        [DirectMethod]
        public void DoNothing()
        {
        }
        [DirectMethod]
        public void SaveAndRefresh(object sender, DirectEventArgs e)
        {
            Notification.Show(new NotificationConfig
            {
                Title = "Información",
                Icon = Icon.Accept,
                Html = "El registro fué Guardado Exitosamente",
                HideDelay = 3000
            });
            dsProject.Reload();
        }
        protected void BtnNewClick(object sender, DirectEventArgs e)
        {
            try
            {
                Logger.Info();
                ProjectWindow.Title = "Nuevo Proyecto";
                ProjectWindow.Show();
                var loader = new ComponentLoader();
                loader.AutoLoad = true;
                loader.Url = "ProjectEdit.aspx";
                ProjectWindow.LoadContent(loader);

            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodBase.GetCurrentMethod(), ex);
            }

        }
        protected void ToExcel(object sender, EventArgs e)
        {
            string json = GridData.Value.ToString();
            var eSubmit = new StoreSubmitDataEventArgs(json, null);
            var xml = eSubmit.Xml;

            Response.Clear();
            Response.ContentType = "application/vnd.ms-excel";
            Response.AddHeader("Content-Disposition", "attachment; filename=Proyectos.xls");
            var xtExcel = new XslCompiledTransform();
            xtExcel.Load(Server.MapPath("..\\ExportConfig\\Excel.xsl"));
            xtExcel.Transform(xml, null, Response.OutputStream);
            Response.End();
        }
    }
}